package WebSum6;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 需求：统计2022年各省份内地居民结婚数
 * 分析：自定义一个对象WebCountBean,用于封装省份和年份，注意需要实现hadoop的序列化机制
 *      以WebCountBean作为map阶段输出的key，内地居民结婚登记数作为value
 */
public class WebSumMapper extends Mapper<LongWritable, Text, WebCountBean, FloatWritable> {
    int i = 0;
    WebCountBean outKey = new WebCountBean();
    FloatWritable outValue = new FloatWritable();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, WebCountBean, FloatWritable>.Context context) throws IOException, InterruptedException {
        i++;
        if (key.get() == 0) {
            return;
        }
        String[] fileds = value.toString().split(",");
        /**
         * 北京市，东城区,2022年,0.53,0.52,0.62,0.47,0.01,0.25,148,
         * 提取数据  省份、年份、内地居民结婚登记数
         */
        String province = fileds[0];
        String year = fileds[2];
        outKey.set(province,year);

            //获取内地居民结婚数
            System.out.println("1:"+i+"2:"+value.toString());
            float web_num = Float.parseFloat(fileds[4]);
            outValue.set(web_num);

            context.write(outKey,outValue);
        }
    }

